#!/usr/bin/env python
# encoding: utf-8

print "keystrokes"

# built-in stuff
import unittest
import sys
sys.path.append("../")
# home made stuff
from scriptgoodies import getTestingEnvironment
# stuff to test
from cwkeys_mac import *

class MacKeyStrokesTest(unittest.TestCase):
	
	def setUp(self):
		env = getTestingEnvironment("cwkeys_mac.json","fr_cwkeys_mac.json")
		self.parse = MacKeyStrokes(env).parse
	
	def testSimpleKey(self):
		self.assertEqual(self.parse(u"a"),u'keystroke "a"')
	
	def testSimpleKeyWithModifier(self):
		self.assertEqual(self.parse(u"ctrl b"),u'keystroke "b" using control down')
	
	def testAlternativeForms(self):
		self.assertEqual(self.parse(u"control b"),u'keystroke "b" using control down')
		self.assertEqual(self.parse(u"Ctrl b"),u'keystroke "b" using control down')
		self.assertEqual(self.parse(u"CTRL B"),u'keystroke "B" using control down')
		self.assertEqual(self.parse(u"CTRL   B"),u'keystroke "B" using control down')
		self.assertEqual(self.parse(u"CTRL   B   "),u'keystroke "B" using control down')
		self.assertEqual(self.parse(u"   CTRL   B   "),u'keystroke "B" using control down')
	
	def testSimpleKeyWithModifiers(self):
		self.assertEqual(self.parse(u"command shift c"),u'keystroke "c" using command down & shift down')
		self.assertEqual(self.parse(u"control alt espace"),u'keystroke " " using control down & option down')
		self.assertEqual(self.parse(u"control alt parenthèse gauche"),u'keystroke "(" using control down & option down')
		self.assertEqual(self.parse(u"control alt '"),u"""keystroke "'" using control down & option down""")
		self.assertEqual(self.parse(u'control alt "'),ur'keystroke "\"" using control down & option down')
	
	def testCodeKeys(self):
		self.assertEqual(self.parse(u"je suis de retour mardi"),u'keystroke "je suis de"\nkeystroke return\nkeystroke "mardi"')
	
	def testCodeKeyWithModifiers(self):
		self.assertEqual(self.parse(u"control alt del"),u'key code 51 using control down & option down')
	
	def testSeveralSimpleKeysWithModifiers(self):
		self.assertEqual(self.parse(u"control alt abc"),u'keystroke "abc" using control down & option down')
	
	def testComplexSequences(self):
		self.assertEqual(self.parse(u"Command tab commande ac cmd TAB command avsa suppr"),u'keystroke tab using command down\nkeystroke "ac" using command down\nkeystroke tab using command down\nkeystroke "avsa" using command down\nkey code 117')
	
	def testEscapeSequences(self):
		self.assertEqual(self.parse(u"délai 42"),u'delay 42')
		self.assertEqual(self.parse(u"délai 4.2"),u'delay 4.2')
		self.assertEqual(self.parse(u"cmd tab attendre 1 cmd tab"),u'keystroke tab using command down\ndelay 1\nkeystroke tab using command down')
		self.assertEqual(self.parse(u"brut del"),u'keystroke "del"')
	
	def testEscapedOrInoperantEscapeSequences(self):
		self.assertEqual(self.parse(u"cmd attendre"),u'keystroke "attendre" using command down')
		self.assertEqual(self.parse(u"cmd attendre s"),u'keystroke "attendre" using command down\nkeystroke "s"')
		self.assertEqual(self.parse(u"brut brut del"),u'keystroke "brut"\nkey code 51')
		self.assertEqual(self.parse(u"je suis de brut retour"),u'keystroke "je suis de retour"')
		self.assertEqual(self.parse(u"je suis de brut retour mardi"),u'keystroke "je suis de retour mardi"')
		self.assertEqual(self.parse(u"une bouteille de brut brut"),u'keystroke "une bouteille de brut"')
		self.assertEqual(self.parse(u"une bouteille de brut brut brut brut"),u'keystroke "une bouteille de brut brut"')
		self.assertEqual(self.parse(u"Bonjour, je suis de brut retour mardi."),u'keystroke "Bonjour"\nkey code 46\nkeystroke " je suis de retour mardi"\nkey code 43 using shift down\nkeystroke ""')
	
	def testStrings(self):
		self.assertEqual(self.parse(u"several words with spaces"),u'keystroke "several words with spaces"')
		self.assertEqual(self.parse(u"several words with   several  spaces"),u'keystroke "several words with several spaces"')
		self.assertEqual(self.parse(u"shift several words with spaces"),u'keystroke "several" using shift down\nkeystroke "words with spaces"')
		self.assertEqual(self.parse(u"several words cmd tab"),u'keystroke "several words"\nkeystroke tab using command down')
	
	def testLimitations(self):
		self.assertEqual(self.parse(u"il dit : « non »."),u'keystroke "il dit : « non »"\nkey code 43 using shift down\nkeystroke ""')
		self.assertEqual(self.parse(u"commande :"),u'keystroke ":" using command down')
	

if __name__ == '__main__':
	unittest.main()


